import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:laohuangli/api.dart';
import 'package:laohuangli/calendar_modal.dart';
import 'package:laohuangli/page.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '老黄历',
      theme: ThemeData(
        primarySwatch: Colors.brown,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: '老黄历'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var _data = null;
  var _dateFormatter = new DateFormat('yyyyMMdd');

  @override
  void initState() {
    var __date = _dateFormatter.format(DateTime.now());
    huangli(__date).then((res) {
      setState(() {
        _data = res;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        elevation: 0,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.date_range),
            tooltip: '日历',
            onPressed: () {
              CalendarModal.show(
                context: context,
                date: DateTime.now(),
                onChange: (date) {
                  setState(() {
                    _data = null;
                  });
                  huangli(_dateFormatter.format(date)).then((res) {
                    setState(() {
                      _data = res;
                    });
                  });
                },
              );
            },
          )
        ],
      ),
      body: Container(
        decoration: BoxDecoration(color: Color(0xFFede7de)),
        child: SingleChildScrollView(child: ContentPage(data: _data)),
      ),
    );
  }
}
